#!/usr/bin/perl 
use strict;

use DBI;
use CGI;
# use TTrakerSite;
use TTraker;
my $dbh;
initialize_dbi;

print_header;

my $form_ref = &parse_input;

my %FORM_DATA = %$form_ref; 
my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job_id = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $job = $FORM_DATA{'jobNum'};
# my $task = $FORM_DATA{'taskNum'};
my $batch =  $FORM_DATA{'batNum'};
my $cli = $FORM_DATA{'cliId'};
my $stamp = $FORM_DATA{'stmp'};
my $activity = $FORM_DATA{'activity'};
my $sent_from = $ENV{'REMOTE_ADDR'};

my $code = "TASK";

# print "$num, $name, $job_id, $loc<br/>";

my $now = get_local_time($loc);
my $first;
my $last;
my $message;
my $info_msg = "";
my $warn_msg = "";
if ( $job == "" ) {
   $warn_msg = "NO JOB OPEN! Can not log tasks.";
   & trak_05_emp_log_in_no_job_open;
}elsif ($cli eq "BREAK" || $cli eq "LUNCH" || $cli eq "IDLE") {
   $warn_msg = "$name is currently on $cli! Can not logg tasks.";
} else {
   $info_msg = "Tasks Logged! $cli job is stil open.";
   my $task = get_new_count('TASK');			  
   foreach my $key (keys %FORM_DATA) {
      if ($key =~ /^task_/ && $FORM_DATA{$key} ne "" ) {
         my $code = $key;
         $code =~ s/task_//;
         my $vol = $FORM_DATA{$key};
         generate_task_event ($job, $task, $code, $vol, $now);
      } elsif ($key =~ /^task_/) {
#        do not print tasks that have no value
      } else {   
#        print "KEY: $key - $FORM_DATA{$key}<br/>";   
      } 
   }
   	update_task_std_time ($job);
	log_job_event ($job, $now);
	if ( $activity == 2 ) {
   		close_current_job ($job);
   		create_idle_event ($num, $now);
   		update_job_headers_with_time ($job);
	} elsif ($activity == 8) {
		close_current_job ($job);
		create_idle_event ($num, $now);
		update_job_headers_with_time ($job);
		clear_batch_jobs_from_hold ($batch);
	} else {
	   update_start_job_open ($job, $now);
	   update_job_headers_with_time ($job);
	}
#   & trak_04_emp_log_in;
	end_system_time ($num, "TASK", $now);
	activity_screen_from_pm ($num, $name, $job_id, $loc);

}


db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

